<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <!-- 引入 bootstrap 样式表 -->
  <link rel="stylesheet" href="./css/bootstrap.min.css" />
  <link rel="stylesheet" href="../nprogress.css">
  <style>
    body {
      padding-top: 50px;
    }
  </style>
</head>

<body>
  <!-- 栅格系统 -->
  <div class="container">
    <div class="d-flex justify-content-between align-items-center">
      <h1>图书管理</h1>
      <button class="btn btn-success btn-sm" data-bs-toggle="modal" data-bs-target="#addModal">添加</button>
    </div>
    <table class="table table-bordered table-striped table-dark table-hover text-center">
      <thead>
        <!-- 表头行 -->
        <tr>
          <th scope="col">Id</th>
          <th scope="col">书名</th>
          <th scope="col">作者</th>
          <th scope="col">出版社</th>
          <th scope="col">操作</th>
        </tr>
      </thead>
      <tbody>
        <!-- 表格中的每一行 -->

      </tbody>
    </table>
  </div>
  <!-- add Modal -->
  <div class="modal fade" id="addModal">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">添加图书</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <form id="addForm" class="p-3">
            <!-- 书名 -->
            <div class="mb-3">
              <label class="form-label">书名</label>
              <input type="text" name="bookname" class="form-control" placeholder="请输入图书名称" name="bookname" />
            </div>
            <!-- 作者 -->
            <div class="mb-3">
              <label class="form-label">作者</label>
              <input type="text" name="author" class="form-control" placeholder="请输入作者名字" name="author" />
            </div>
            <!-- 出版社 -->
            <div class="mb-3">
              <label class="form-label">出版社</label>
              <input type="text" name="publisher" class="form-control" placeholder="请输入出版社名称" name="publisher" />
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
          <button type="button" class="btn btn-primary" id="addBtn">确认</button>
        </div>
      </div>
    </div>
  </div>
  <!-- edit Modal -->
  <div class="modal fade" id="editModal">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">编辑图书</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <form id="editForm" class="p-3">
            <input type="hidden" name="id" />
            <!-- 书名 -->
            <div class="mb-3">
              <label class="form-label">书名</label>
              <input type="text" name="bookname" class="form-control" placeholder="请输入图书名称" name="bookname" />
            </div>
            <!-- 作者 -->
            <div class="mb-3">
              <label class="form-label">作者</label>
              <input type="text" name="author" class="form-control" placeholder="请输入作者名字" name="author" />
            </div>
            <!-- 出版社 -->
            <div class="mb-3">
              <label class="form-label">出版社</label>
              <input type="text" name="publisher" class="form-control" placeholder="请输入出版社名称" name="publisher" />
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
          <button type="button" class="btn btn-primary" id="editBtn">确认</button>
        </div>
      </div>
    </div>
  </div>
  <!-- 引入 lib 目录下的 bootstrap 和 axios -->
  <script src="./lib/axios.js"></script>
  <script src="./lib/form-value.js"></script>
  <script src="./lib/bootstrap.min.js"></script>
  <script src="../nprogress.js"></script>
  <script>
    // 添加请求拦截器
    axios.interceptors.request.use(function (config) {
      // 在发送请求之前做些什么
      NProgress.start()
      return config;
    }, function (error) {
      // 对请求错误做些什么
      return Promise.reject(error);
    });

    // 添加响应拦截器
    axios.interceptors.response.use(function (response) {
      // 2xx 范围内的状态码都会触发该函数。
      // 对响应数据做点什么
      NProgress.done()
      return response;
    }, function (error) {
      // 超出 2xx 范围的状态码都会触发该函数。
      // 对响应错误做点什么
      NProgress.done()
      return Promise.reject(error);
    });

    axios.defaults.baseURL = 'http://www.itcbc.com:3006';
    function render() {
      axios.get('/api/getbooks', {
        params: {
          appkey: 'xiaomeng'
        }
      }).then(({ data }) => {
        const arr = data.data.map(item => {
          const { id, bookname, author, publisher } = item
          return `<tr>
                  <th scope="row">${id}</th>
                  <td>${bookname}</td>
                  <td>${author}</td>
                  <td>${publisher}</td>
                  <td>
                    <button data-id=${id} type="button" class="btn btn-link btn-sm btn-delete">删除</button>
                    <button data-id=${id} data-bookname=${bookname} data-author=${author} data-publisher=${publisher} type="button" class="btn btn-link btn-sm btn-update">编辑</button>
                  </td>
                </tr>`
        })
        document.querySelector('tbody').innerHTML = arr.join('')
      }).catch((error) => {
        alert(error.response.data.message)
      })
    }
    render()

    const addModal = new bootstrap.Modal(document.querySelector('#addModal'))
    document.querySelector('.btn-success').addEventListener('click', function () {
      addModal.show()
    })
    document.querySelector('#addBtn').addEventListener('click', function () {
      // const bookname = document.querySelector('#addForm [name=bookname]').value
      // const author = document.querySelector('#addForm [name=author]').value
      // const publisher = document.querySelector('#addForm [name=publisher]').value
      const datas = val(document.querySelector('#addForm'))
      datas.appkey = 'xiaomeng'
      axios.post('/api/addbook',
        datas
      ).then(({ data }) => {
        if (data.status === 0) {
          addModal.hide()
          render()
          document.querySelector('#addForm').reset()
        }
      }).catch((error) => {
        alert(error.response.data.message)
      })
    })
    const editModal = new bootstrap.Modal(document.querySelector('#editModal'))
    document.querySelector('tbody').addEventListener('click', function (e) {
      if (e.target.classList.contains('btn-delete')) {
        axios.delete('/api/delbook', {
          params: {
            id: e.target.dataset.id,
            appkey: 'xiaomeng'
          }
        }).then(({ data }) => {
          if (data.status === 0) {
            render()
          }
        }).catch((error) => {
          alert(error.response.data.message)
        })
      }
      if (e.target.classList.contains('btn-update')) {
        editModal.show()
        const datas = e.target.dataset
        for (let k in datas) {
          document.querySelector(`#editForm [name=${k}]`).value = datas[k]
        }
      }
    })

    document.querySelector('#editBtn').addEventListener('click', function () {
      // const bookname = document.querySelector('#editForm [name=bookname]').value
      // const author = document.querySelector('#editForm [name=author]').value
      // const publisher = document.querySelector('#editForm [name=publisher]').value
      // const id = document.querySelector('#editForm [name=id]').value
      const datas = val(document.querySelector('#editForm'))
      datas.appkey = 'xiaomeng'
      axios.put('/api/updatebook',
        datas
      ).then(({ data }) => {
        if (data.status === 0) {
          editModal.hide()
          render()
        }
      }).catch((error) => {
        alert(error.response.data.message)
      })
    })
  </script>
</body>

</html>